/*
 * Copyright (c) Ulrich Hilger, http://dev.uhilger.de, all rights reserved.
 *
 * Published under the terms and conditions of the Apache License 2.0,
 * see http://www.apache.org/licenses/LICENSE-2.0
 */

package de.uhilger.servicehub.ctrl.action;

import de.uhilger.servicehub.ServiceHub;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;

/**
 * An action to stop the server object of application ServiceHub
 *
 * @author Ulrich Hilger, <a href="http://dev.uhilger.de" target="_blank">http://dev.uhilger.de</a>
 * @author Published under the terms and conditions of
 * the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License 2.0</a>
 */
public class StopServerAction extends AbstractAction {

  private ServiceHub owner;

  /**
   * Create a new object of class StopServerAction
   * @param owner  the owner of this action
   */
  public StopServerAction(ServiceHub owner) {
    super();
    this.owner = owner;
    putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.CTRL_MASK));
  }

  /**
   * Stop the server of the ServiceHub applicaiton that owns this action
   *
   * @param e the event that fired this action
   */
  public void actionPerformed(ActionEvent e) {
    try {
      ServiceHub.log("Attempting to stop server.", getClass(), Level.INFO);
      owner.getServer().stop();
      ServiceHub.log("Server started.", getClass(), Level.INFO);
    } catch (Exception ex) {
      ServiceHub.logStackTrace(ex, getClass());
    }
  }

}
